<?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace pays\Gateways\wechatpay;
class Wechatpay{
    protected $config;
    
    public function __construct($config)
    {
        $this->config = $config;
    }
    //小程序支付
    public function miniapp($order=array()){
        if(!is_array($order)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($order,["openid"])){
            die("请传入Openid!");
        }
        if(!$this->check_params($order,["total"])){
            die("请传入付款金额!");
        }
        if(!$this->check_params($order,["ordersn"])){
            die("请传入订单编号!");
        }
        if(!$this->check_params($order,["subject"])){
            die("请传入付款描述参数!");
        }
        if(!$this->check_params($order,["notify"])){
            die("请传入回调地址参数!");
        }
        $params = array(
            "openid"=>$order["openid"],
            "body"=>$order["subject"],
            "out_trade_no"=>$order["ordersn"],
            "total_fee"=>$order["total"]*100,
            "notify_url"=>$order["notify"],
            "trade_type"=>"JSAPI",
        );
        $WechatSDK = new WechatSDK();
        return $WechatSDK::unifiedOrder($this->config,$params);
    }
    //扫码支付
    public function qrcode($order=array()){
        if(!is_array($order)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($order,["total"])){
            die("请传入付款金额!");
        }
        if(!$this->check_params($order,["ordersn"])){
            die("请传入订单编号!");
        }
        if(!$this->check_params($order,["subject"])){
            die("请传入付款描述参数!");
        }
        if(!$this->check_params($order,["notify"])){
            die("请传入回调地址参数!");
        }
        $params = array(
            "body"=>$order["subject"],
            "out_trade_no"=>$order["ordersn"],
            "total_fee"=>$order["total"]*100,
            "notify_url"=>$order["notify"],
            "trade_type"=>"NATIVE",
        );
        $WechatSDK = new WechatSDK();
        return $WechatSDK::unifiedOrder($this->config,$params);
    }
	//App支付
    public function app($order=array()){
        if(!is_array($order)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($order,["total"])){
            die("请传入付款金额!");
        }
        if(!$this->check_params($order,["ordersn"])){
            die("请传入订单编号!");
        }
        if(!$this->check_params($order,["subject"])){
            die("请传入付款描述参数!");
        }
        if(!$this->check_params($order,["notify"])){
            die("请传入回调地址参数!");
        }
        $params = array(
            "body"=>$order["subject"],
            "out_trade_no"=>$order["ordersn"],
            "total_fee"=>$order["total"]*100,
            "notify_url"=>$order["notify"],
            "trade_type"=>"APP",
        );
        $WechatSDK = new WechatSDK();
        return $WechatSDK::unifiedOrder($this->config,$params);
    }
    //订单查询
    public function query($input = array()){
        if(!is_array($input)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($input,["out_trade_no"])){
            die("请传入支付单号!");
        }
        $WechatSDK = new WechatSDK();
        return $WechatSDK::orderQuery($this->config,$input);
    }
    //订单退款
    public function refund($input = array()){
        if(!is_array($input)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($input,["order_sn"])){
            die("请传入订单编号!");
        }
        if(!$this->check_params($input,["total_fee"])){
            die("请传入订单支付金额!");
        }
        if(!$this->check_params($input,["refund_fee"])){
            die("请传入订单退款金额!");
        }
        $WechatSDK = new WechatSDK();
        return $WechatSDK::refund($this->config,$input);
    }
	//转账到个人
    public function transfers($input = array()){
        if(!is_array($input)){
            die("请传入数组参数!");
        }
        if(!$this->check_params($input,["order_sn"])){
            die("请传入商户订单编号!");
        }
        if(!$this->check_params($input,["total_fee"])){
            die("请传入转账金额!");
        }
        if(!$this->check_params($input,["openid"])){
            die("请传入OPENID!");
        }
        $WechatSDK = new WechatSDK();
        return $WechatSDK::transfers($this->config,$input);
    }
    protected function check_params($data, $c = array())
    {
        if (empty($c)) {
            return $data;
        }
        foreach ($c as $k => $v) {
            if (array_key_exists($v, $data) == false) {
                return false;
            }
        }
        return $data;
    }
}


